// miniprogram/pages/allLogs/allLogs.js
const db = wx.cloud.database()
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    number: 90,
    type: false,
    typeList:[],
    typeName:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const { number, type,typeName } = this.data
    const that = this
    var list = []
    const _ = db.command
    db.collection('logs')
      .orderBy(type ? 'like' : 'createTime', 'desc')
      .where({
        open: true,
        number: _.lte(number),
        // type:!!typeName?typeName:"",
      }).get().then(res => {
        console.log(res)
        list = res.data.map(item => {
          return {
            ...item, likeStatus: 0
          }
        })
        db.collection('likes').where({
          type: true,
          _openid: app.globalData.openid// 填入当前用户 openid
        }).get().then(res => {
          const like = res.data.map(item => item.logId)
          list.forEach(item => {
            if (like.includes(item._id)) {
              item.likeStatus = 2
            }
          })
          db.collection('likes').where({
            type: false,
            _openid: app.globalData.openid// 填入当前用户 openid
          }).get().then(res => {
            const nolike = res.data.map(item => item.logId)
            list.forEach(item => {
              if (nolike.includes(item._id)) {
                item.likeStatus = 1
              }
            })
            that.setData({ list })
          })
        })
      })


  },
  slider4change: function (e) {
    this.setData({ number: e.detail.value })
    this.onShow()
  },
  changeType: function (e) {
    const { type } = this.data
    this.setData({ type: !type })
    this.onShow()
  },


  like: function (e) {
    const { id } = e.target
    const { list } = this.data
    const { like } = list.find(item => item._id === id)
    const { likeStatus } = list.find(item => item._id === id)
    if (likeStatus === 2) {
      wx.showToast({
        title: '知道了知道了',
        icon: "none"
      })
      return false
    }
    db.collection('logs').doc(id).update({
      data: {
        like: like + 1
      },
    })
    this.changeLike(id, true)
  },

  nolike: function (e) {
    const { id } = e.target
    const { list } = this.data
    const { nolike } = list.find(item => item._id === id)
    const { likeStatus } = list.find(item => item._id === id)
    if (likeStatus === 1) {
      wx.showToast({
        title: '知道了知道了',
        icon: "none"
      })
      return false
    }
    db.collection('logs').doc(id).update({
      data: {
        nolike: nolike + 1
      },
    })
    this.changeLike(id, false)
  },
  changeLike: function (id, type) {
    const that = this
    db.collection('likes').where({
      logId: id,
      _openid: app.globalData.openid// 填入当前用户 openid
    }).count().then(res => {
      if (res.total == 0) {
        db.collection('likes').add({
          // data 字段表示需新增的 JSON 数据
          data: {
            logId: id,
            type
          }
        })
        that.onShow()
        return false
      } else {
        that.reduceLike(id, type)
        db.collection('likes').where({
          logId: id
        }).get().then(res => {
          db.collection('likes').doc(res.data[0]._id).set({
            data: {
              logId: id,
              type
            }
          })
            .then(() => {
              wx.showToast({
                title: type ? '喜欢!!' : '一般~~',
                icon: "none"
              })
              that.onShow()
            })
            .catch(console.error)
        })
      }
    })


  },
  reduceLike: function (id, type) {
    const { list } = this.data
    const { like } = list.find(item => item._id === id)
    const { nolike } = list.find(item => item._id === id)
    if (type) {
      db.collection('logs').doc(id).update({
        data: {
          nolike: nolike - 1
        },
      })
    } else {
      db.collection('logs').doc(id).update({
        data: {
          like: like - 1
        },
      })
    }

  },
  showTypes:function(){
    const that=this
    db.collection('types').where({}).get().then(res => {
      that.setData({typeList:res.data})
    })
  },
  chooseType:function(e){
    this.setData({typeList:[],typeName:e.target.dataset.typename})
    this.onShow()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})